package com.mjh.config;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.mjh.util.JwtUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author MJH
 * @date 2021-12-21
 * @Description 用户认证
 */
@Component
public class JwtInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        if(token!=null){
            //验证令牌
            boolean verifi = JwtUtil.verificationToken(token);
            if(!verifi){
                response.setStatus(401);//用户未认证
                return false;
            }
            return true;
        }
        response.setStatus(401);//用户未认证
        return false;
    }
}
